import re

import matplotlib.pyplot as plt  #导入库定义为plt
import random
from pylab import mpl  #设置中文字体
from operator import itemgetter  #itemgetter用来去dict中的key，省去了使用lambda函数
from itertools import groupby  #itertool还包含有其他很多函数，比如将多个list联合起来。。
from pprint import pprint
# 加载数据库
from heiben_py.main.util_db import DataBaseHandle

DbHandle = DataBaseHandle()
fp1 = open(
    "C:\\WorkSpace\\gitee\\heiben_py\\heiben_py\\main\\TestLogFile\\topinfo.log"
)
str = fp1.read()
data = str.split("|||||||||||||||||||||||||||||||||||||||||||||||||||||||||")
del data[0]
data2 = []
h_id_s = DbHandle.selectDb_2(
    "SELECT id from machine_hardware_information where hardware_ip='182.92.8.158' limit 1 "
)
id = h_id_s[0]['id']
t_id_s = DbHandle.selectDb_2(
    'select task_id from task where h_id = 4  and task_id =8')
task_id = t_id_s[0]['task_id']
sql1 = "INSERT INTO `info` (`h_id`, `t_id`, `date`, `load_average_1min`, `load_average_5min`, `load_average_15min`, `Tasks_total`, `Tasks_running`, `Tasks_sleeping`, `Tasks_stopped`, `Tasks_zombie`, `cpu_us`, `cpu_sy`, `cpu_ni`, `cpu_id`, `cpu_wa`, `cpu_hi`, `cpu_st`, `mem_total`, `cpu_si`, `mem_free`, `mem_used`, `mem_buffCache`, `Swap_total`, `Swap_free`, `Swap_used`, `Swap_avail`, `free_mem_total`, `free_mem_used`, `free_mem`, `free_mem_shared`, `free_mem_buffCache`, `free_swap_total`, `free_mem_available`, `free_swap_used`, `free_swap`, `gpu_num`, `gpu_model`, `gpu_fan`, `gpu_temperature`, `gpu_Perf`, `gpu_Pwr_Usage`, `gpu_GPU_Util`) VALUES (%s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s);"
for item in data:
    s1 = item.split("********************************************************")
    # top命令下的取值
    top = s1[1].split(" ")
    user = top[8]  # 当前有几个用户登录系统
    # 平均负载时长
    load_average_1min = top[13].split(",")[0]
    load_average_5min = top[14].split(",")[0]
    load_average_15min = top[15].split("\n")[0]
    # Tasks 任务（进程）
    int_value = top[16].split("\n")[0]  # 总计进程
    Tasks_total = float(int_value)
    for i in range(0, 10000):
        print(i, top[20:23])
        i += 1
    int_value_2 = top[21]  # 运行状态
    Tasks_running = int(int_value_2)
    int_value_3 = top[23]  # 睡眠状态
    Tasks_sleeping = int(int_value_3)
    int_value_4 = top[27]  # 停止状态
    Tasks_stopped = int(int_value_4)
    int_value_5 = top[31]  # 僵尸状态
    Tasks_zombie = int(int_value_5)

    # cpu 状态 单位KiB
    int_value_6 = top[34]  # 用户空间占用CPU的百分比
    cpu_us = float(int_value_6)
    int_value_7 = top[37]  # 内核空间占用CPU的百分比
    cpu_sy = float(int_value_7)
    int_value_8 = top[40]  # 改变过优先级的进程占用CPU的百分比
    cpu_ni = float(int_value_8)
    int_value_9 = top[42]  # 空闲CPU百分比
    cpu_id = float(int_value_9)
    int_value_10 = top[45]  # IO等待占用CPU的百分比
    cpu_wa = float(int_value_10)
    int_value_11 = top[48]  # 硬中断（Hardware IRQ）占用CPU的百分比
    cpu_hi = float(int_value_11)
    int_value_12 = top[51]  # 软中断（Software Interrupts）占用CPU的百分比
    cpu_si = float(int_value_12)
    int_value_13 = top[54]  #
    cpu_st = float(int_value_13)

    # mem 状态  单位KiB
    t = top[58].split("+")
    int_value_14 = t[0]  # 物理内存总量（32GB)-- 取的是kib
    mem_total = float(int_value_14) / 1024 / 1024
    f = top[59].split("+")
    int_value_15 = f[0]  #  使用中的内存总量（14GB）-- 取的是kib
    mem_free = float(int_value_15) / 1024 / 1024
    int_value_16 = top[60]  # 空闲内存总量（18GB）-- 取的是kib
    mem_used = 0
    if int_value_16 != '':
        mem_used = float(int_value_16) / 1024 / 1024
    int_value_17 = top[61]  # 缓存的内存量 （169M）-- 取的是kib
    mem_buffCache = float(int_value_17) / 1024 / 1024
    # Swap 交换分区信息
    Swap_total = top[73]  # 交换区总量（32GB）
    Swap_free = top[82]  # 使用的交换区总量（0K）
    Swap_used = top[91]  # 空闲交换区总量（32GB）
    a = top[93].split("+")
    Swap_avail = a[0]  # 缓冲的交换区总量（3.6GB）

    # free命令下的取值
    free_i = s1[2].split(" ")
    # mem

    free_mem_total = 0
    if ("T" in free_i[52]):
        int_value_18 = free_i[52].split("T")[0]
        free_mem_total = float(int_value_18) * 1024
    else:
        int_value_18 = free_i[52].split("G")[0]
        free_mem_total = float(int_value_18)

    free_mem_used = 0
    if ("T" in free_i[60]):
        int_value_19 = free_i[60].split("T")[0]
        free_mem_used = float(int_value_19) * 1024
    else:
        int_value_19 = free_i[60].split("G")[0]
        free_mem_used = float(int_value_19)

    int_value_20 = free_i[68].split("G")[0]
    free_mem = int(int_value_20)

    int_value_21 = free_i[77].split("M")[0]
    free_mem_shared = int(int_value_21)

    int_value_22 = free_i[86].split("G")[0]
    free_mem_buffCache = float(int_value_22)

    int_value_23 = free_i[94].split("G")[0]
    free_mem_available = int(int_value_23)

    # swap
    int_value_24 = free_i[106].split("B")[0]
    free_swap_total = int(int_value_24)

    int_value_25 = free_i[116].split("B")[0]
    free_swap_used = int(int_value_25)

    int_value_26 = free_i[126].split("B")[0]
    free_swap = int(int_value_26)

    gpu_num = 0  # GPU序号
    gpu_model = 0  # GPU名字
    gpu_fan = 0  # 风扇转速
    gpu_temperature = 0  # GPU温度
    gpu_Perf = 0  # 是性能状态，从P0到P12，P0表示最大性能，P12表示最小性能地
    gpu_Pwr_Usage = 0  # 使用的耗能
    gpu_GPU_Util = 0  # 显存核心利用率

    if "|===============================+======================+======================|" in s1[
            5]:
        # GPU下的取值
        gpu = s1[5].split(
            "|===============================+======================+======================|"
        )
        gpu_nei = gpu[1].split(
            "+-------------------------------+----------------------+----------------------+"
        )
        for i in range(len(gpu_nei)):
            gpu_order_1111 = gpu_nei[i].split(
                "+-------------------------------+----------------------+----------------------+"
            )
            for n in range(len(gpu_order_1111)):
                nn = gpu_order_1111[n].split(" ")
                if nn[3] != "":
                    gpu_num = nn[3]  # GPU序号
                    gpu_model = nn[4] + nn[5] + nn[6] + nn[7]  # GPU名字
                    int_value_27 = nn[34].split("%")[0]  # 风扇转速
                    gpu_fan = int(int_value_27)  # 风扇转速

                    int_value_28 = nn[37].split("C")[0]  # GPU温度
                    gpu_temperature = int(int_value_28)  # GPU温度

                    gpu_Perf = nn[41]  # 是性能状态，从P0到P12，P0表示最大性能，P12表示最小性能地
                    int_value_29 = nn[45].split("W")[0]  # 使用的耗能
                    gpu_Pwr_Usage = int(int_value_29)  # 使用的耗能
                    int_value_30 = nn[63].split("%")[0]  # 显存核心利用率
                    gpu_GPU_Util = int(int_value_30)  # 显存核心利用率

                    data2.append(
                        (id, task_id, s1[0], load_average_1min,
                         load_average_5min, load_average_15min, Tasks_total,
                         Tasks_running, Tasks_sleeping, Tasks_stopped,
                         Tasks_zombie, cpu_us, cpu_sy, cpu_ni, cpu_id, cpu_wa,
                         cpu_hi, cpu_st, mem_total, cpu_si, mem_free, mem_used,
                         mem_buffCache, Swap_total, Swap_free, Swap_used,
                         Swap_avail, free_mem_total, free_mem_used, free_mem,
                         free_mem_shared, free_mem_buffCache, free_swap_total,
                         free_mem_available, free_swap_used, free_swap,
                         gpu_num, gpu_model, gpu_fan, gpu_temperature,
                         gpu_Perf, gpu_Pwr_Usage, gpu_GPU_Util))

                    if (len(data2) == 400000):
                        DbHandle.insertDBmany(sql1, data2)
                        data2 = []
    else:
        data2.append(
            (id, task_id, s1[0], load_average_1min, load_average_5min,
             load_average_15min, Tasks_total, Tasks_running, Tasks_sleeping,
             Tasks_stopped, Tasks_zombie, cpu_us, cpu_sy, cpu_ni, cpu_id,
             cpu_wa, cpu_hi, cpu_st, mem_total, cpu_si, mem_free, mem_used,
             mem_buffCache, Swap_total, Swap_free, Swap_used, Swap_avail,
             free_mem_total, free_mem_used, free_mem, free_mem_shared,
             free_mem_buffCache, free_swap_total, free_mem_available,
             free_swap_used, free_swap, gpu_num, gpu_model, gpu_fan,
             gpu_temperature, gpu_Perf, gpu_Pwr_Usage, gpu_GPU_Util))

        if (len(data2) == 400000):
            DbHandle.insertDBmany(sql1, data2)
            data2 = []

if len(data2) > 0:
    DbHandle.insertDBmany(sql1, data2)